/*
 * @Description: 
 * @Author: zheng yiliang
 * @Date: 2021-03-28 16:37:23
 * @LastEditTime: 2021-04-01 22:27:05
 * @LastEditors: zheng yiliang
 */
const { SuccessModel, ErrorModel } = require('../util')
const { getToken } = require('../util/jwt')

module.exports = (req, res, next) => {
    // res.json(new ErrorModel('尚未登录'))
    const _id = getToken(req, res)
    if (_id instanceof Error) {
        if (req.url === '/userInfo') {
            return res.json(new ErrorModel('登录已过期，请重新登录'))
        }
        return res.json(new ErrorModel('登录已过期，请重新登录', '50014'))
    }
    if (_id) {
        req.user_id = _id
        return next()
    }
    return res.json(new ErrorModel('尚未登录'))
}